import { _decorator, Button, Label, Node, Color } from 'cc';
import { BaseWindow } from '../../../core/runtime/UIFramework';

const { ccclass, property } = _decorator;

/**
 * Toast提示组件
 */
@ccclass('Toast')
export class Toast extends BaseWindow {
    @property(Label)
    private messageLab: Label | null = null;

    @property(Node)
    private background: Node | null = null;

    private _autoCloseTime: number = 2; // 默认2秒后自动关闭

    init(): void {
        this._uiName = "Toast";
    }

    onOpen(data?: any): void {
        super.onOpen(data);
        
        if (data) {
            if (this.messageLab) {
                this.messageLab.string = data.message || "";
            }
            
            this._autoCloseTime = data.duration || 2;
            
            // 设置背景颜色
            if (data.type === "success" && this.background) {
                // this.background.color = Color.GREEN;
            } else if (data.type === "error" && this.background) {
                // this.background.color = Color.RED;
            } else if (data.type === "warning" && this.background) {
                // this.background.color = Color.YELLOW;
            }
        }
        
        // 自动关闭
        this.scheduleOnce(() => {
            this.close();
        }, this._autoCloseTime);
    }

    onClose(): void {
        super.onClose();
    }
}